<?php

namespace app\helps;
use app\facade\Redis;
use think\Db;

/**
 * 奖金(团队/直推)操作记录表
 * Class MoneyBonusHelper
 * @package app\helps
 */
class MoneyMtHelper extends MoneyHelperBaser{

    /**
     * mt充值
     * @param $user 发放mt用户
     * @param $num  数量
     * @param $msg 备注
     */
    public function rechargeMt($user,$num){
        $type = self::TYPE_MT;
        $op_type =self::OP_TYPE_MT_RECHARGE;
        $msg="用户充值MT:".$num;
        $res = $this->addUserMoneyAndRecord($user,$num,self::FILED_MT,$msg,$type,$op_type,0);
        if ($res!=1){
            return $res;
        }
        return 1;
    }

    /**
     * 上级用户转赠
     * @param $user
     * @param $toUser
     * @param $num
     * @return int|string
     * @throws \think\Exception
     */
    public function transferMt($user,$toUser,$num){
        $type = self::TYPE_MT;
        $op_type =self::OP_TYPE_MT_TRANSFER_FROM;
        $msg="用户转给:".$toUser["username"]."MT".$num;
        $res = $this->reduceUserMoneyAndRecord($user,$num,self::FILED_MT,$msg,$type,$op_type,0);
        if ($res!=1){
            return $res;
        }
        $type = self::TYPE_MT;
        $op_type =self::OP_TYPE_MT_TRANSFER_TO;
        $msg="来自用户:".$user["username"]."转赠MT".$num;
        $res = $this->addUserMoneyAndRecord($toUser,$num,self::FILED_MT,$msg,$type,$op_type,0);
        if ($res!=1){
            return $res;
        }
        return 1;
    }


    /**
     * 用户挂otc卖单
     * $num 需要做是否是100整数倍限制
     * @param $buyUser 出售用户
     * @param $num 出售mt 数量
     */
    public function otcFeedSell($buyUser,$num) {
        if ($buyUser["status"]==0){
            return "您的账号被禁用,请联系客户解禁";
        }
        if ($buyUser["activity"]==0){
            return "您的账号未激活,请去商城激活";
        }

        $money_mt_otc_sell_min = intval(Redis::hget("system","money_mt_otc_sell_min"));
        if ($num < $money_mt_otc_sell_min){
            return "最小发布数量为".$money_mt_otc_sell_min;
        }
        $fee=0;
        //手续未百分比
        $money_mt_otc_sell_fee = intval(Redis::hget("system","money_mt_otc_sell_fee"));
        if ($money_mt_otc_sell_fee){
            $fee= $money_mt_otc_sell_fee*$num/100;
        }
        $mt =$buyUser[self::FILED_MT];
        $mtforze=$buyUser[self::FILED_MT_FORZE];
        $totleNum = $num+$fee;
        if ($mt<$totleNum){
            return "mt数量不足";
        }
        $uid=$buyUser["id"];
        //1扣除手续费
        if ($fee>0){
            // 2 手续费扣除
            $type = self::TYPE_MT;
            $op_type =self::OP_TYPE_MT_SELL_FEE;
            $msgstr="OTC用户出售MT手续费:".$fee;
            $res = $this->reduceUserMoneyAndRecord($buyUser,$fee,self::FILED_MT,$msgstr,$type,$op_type,0);
            if ($res!=1){
                return $res;
            }
        }
        // 3 扣出售mt
        $type = self::TYPE_MT;
        $op_type =self::OP_TYPE_MT_SELL_FROZEN;
        $msgstr="OTC用户出售MT可用余额扣除".$num;
        $buyUser["mt"]=  $buyUser["mt"] -$fee;
        $res = $this->reduceUserMoneyAndRecord($buyUser,$num,self::FILED_MT,$msgstr,$type,$op_type,0);
        if ($res!=1){
            return $res;
        }
        //4 冻结mt增加
        $type = self::TYPE_MT;
        $op_type =self::OP_TYPE_MT_SELL_FROZEN;
        $msgstr="OTC用户出售MT冻结余额增加".$num;
        $res = $this->addUserMoneyAndRecord($buyUser,$num,self::FILED_MT_FORZE,$msgstr,$type,$op_type,0);
        if ($res!=1){
            return $res;
        }
        return 1;
    }



    /**
     * otc 卖家取消自己的卖单
     * @param $sell otc 卖单
     */
    public function otcSellingCancle($sell) {
        $sellUid = $sell["uid"];
        $sellUser=  Db::table("tb_user")->where("id",$sellUid)->find();
        $uid = $sellUser["id"];
        $num = $sell["sell_num"];
        if ($sellUser["mt_forze"]<$num){
            return "冻结余额不足:".$sellUser["mt_forze"];
        }
        // 返回冻结金额
        //1 冻结减少
        $type = self::TYPE_MT;
        $op_type =self::OP_TYPE_MT_SELL_CANCLE;
        $msgstr="OTC用户取消MT冻结余额减少".$num;;
        $res = $this->reduceUserMoneyAndRecord($sellUser,$num,self::FILED_MT_FORZE,$msgstr,$type,$op_type,0);
        if ($res!=1){
            return $res;
        }
        //2mt 余额增加
        $type = self::TYPE_MT;
        $op_type =self::OP_TYPE_MT_SELL_CANCLE;
        $msgstr="OTC用户取消MT可用余额增加".$num;;
        $res = $this->addUserMoneyAndRecord($sellUser,$num,self::FILED_MT,$msgstr,$type,$op_type,0);
        if ($res!=1){
            return $res;
        }
        return 1;
    }


    /**
     * otc订单确认成功
     * @param $order
     */
    public function otcOrderComfit($order){
        //冻结mt 减少
        $sellUid = $order["seller_uid"];
        $buyerUid = $order["buyer_uid"];
        $sellUser=  Db::table("tb_user")->where("id",$sellUid)->find();
        $buyUser=  Db::table("tb_user")->where("id",$buyerUid)->find();
        $num = $order["buy_num"];
        $type = self::TYPE_MT;
        $op_type =self::OP_TYPE_MT_SELL_SUC;
        $msgstr="OTC用户出售MT成功:MT冻结余额扣除".$num;;
        $res = $this->reduceUserMoneyAndRecord($sellUser,$num,self::FILED_MT_FORZE,$msgstr,$type,$op_type,0);
        if ($res!=1){
            return $res;
        }
        $type = self::TYPE_MT;
        $op_type =self::OP_TYPE_MT_SELL_SUC;
        $msgstr="OTC用户买入MT成功:MT可用余额增加".$num;;
        $res = $this->addUserMoneyAndRecord($buyUser,$num,self::FILED_MT,$msgstr,$type,$op_type,0);
        if ($res!=1){
            return $res;
        }
        return 1;
    }

    /**
     * 发放mt收益
     * @param $user 发放收益用户
     * @param $num  数量
     * @param $msg 备注
     */
    public function mtProfit($user,$num,$msg){
        $type = self::TYPE_MT;
        $op_type =self::OP_TYPE_MT_PROFIT;
        $msgstr="发放合约收益：".":来自".$msg;
        $res = $this->addUserMoneyAndRecord($user,$num,self::FILED_MT,$msgstr,$type,$op_type,0);
        if ($res!=1){
            return $res;
        }
        return 1;
    }


    /**
     * 没有中奖用户发放mt
     * @param $user 发放mt用户
     * @param $num  数量
     * @param $msg 备注
     */
    public function bookFailGetMt($user,$num,$msg){
        $type = self::TYPE_MT;
        $op_type =self::OP_TYPE_MT_BOOK_FAIL;
        $msgstr="预定未中奖赠送".$num.":来自".$msg;
        $res = $this->addUserMoneyAndRecord($user,$num,self::FILED_MT,$msgstr,$type,$op_type,0);
        if ($res!=1){
            return $res;
        }
        return 1;
    }



    /**
     * mt兑换兑换券
     * @param $user
     * @param $mtNum
     * @param $volumeNum
     * @return string
     * @throws \think\Exception
     */
    public function exchangeMtToVolume($user,$mtNum,$volumeNum){
        $type = self::TYPE_MT;
        $op_type =self::OP_TYPE_MT_TO_VOLUME;
        $msgstr="用户用MT".$mtNum.":兑换兑换券".$volumeNum;
        $res = $this->reduceUserMoneyAndRecord($user,$mtNum,self::FILED_MT,$msgstr,$type,$op_type,0);
        if ($res!=1){
            return $res;
        }
        $type = self::TYPE_VOLUME;
        $op_type =self::OP_TYPE_VOLUME_FROM_MT;
        $msgstr="用户用MT".$mtNum.":兑换兑换券".$volumeNum;
        $res = $this->addUserMoneyAndRecord($user,$volumeNum,self::FILED_VOLUME,$msgstr,$type,$op_type,0);
        if ($res!=1){
            return $res;
        }
        return 1;
    }

    /**
     * mt签到赠送
     * @param $user
     * @param $mtNum
     * @param $volumeNum
     * @return string
     * @throws \think\Exception
     */
    public function sginMt($user){
        $type = self::TYPE_MT;
        $op_type =self::OP_TYPE_MT_SIGN;
        $sgin_day_mt_num = floatval(Redis::hget("system","sgin_day_mt_num"));
        if ($sgin_day_mt_num<=0){
            return "签到赠送mt数量小于或者等于0";
        }
        $msgstr="用户签到赠送MT".$sgin_day_mt_num;
        $res = $this->addUserMoneyAndRecord($user,$sgin_day_mt_num,self::FILED_MT,$msgstr,$type,$op_type,0);
        if ($res!=1){
            return $res;
        }
        return 1;
    }

    /**
     * 换购商品的手续费
     * @param $user
     * @param $checkTradePass
     * @return int|string
     */
    public function exchangeGoodsFee($user,$mtNum,$msg){
        $type = self::TYPE_MT;
        $op_type =self::OP_TYPE_MT_FEE;
        if ($mtNum<=0){
            return "手续费为0";
        }
        $msgstr=$msg;
        $res = $this->reduceUserMoneyAndRecord($user,$mtNum,self::FILED_MT,$msgstr,$type,$op_type,0);
        if ($res!=1){
            return $res;
        }
        return 1;
    }

    /**
     * 换购商品的手续费
     * @param $user
     * @param $checkTradePass
     * @return int|string
     */
    public function award($user,$mtNum,$msg){
        $type = self::TYPE_MT;
        $op_type =self::OP_TYPE_MT_AWARD;
        $msgstr=$msg;
        $res = $this->addUserMoneyAndRecord($user,$mtNum,self::FILED_MT,$msgstr,$type,$op_type,0);
        if ($res!=1){
            return $res;
        }
        return 1;
    }

}
